import java.util.Objects;
import java.util.Random;

public class Car extends Thread {

    private String carNumber;
    private final static ParkingLot parkingLot = new ParkingLot(10);
    private long parkingStartTime;

    public Car(String carNumber) {
        this.carNumber = carNumber;
    }

    public String getCarNumber() {
        return carNumber;
    }

    public void setCarNumber(String carNumber) {
        this.carNumber = carNumber;
    }

    public static ParkingLot getParkingLot() {
        return parkingLot;
    }

    public long getParkingStartTime() {
        return parkingStartTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Objects.equals(carNumber, car.carNumber);
    }

    @Override
    public int hashCode() {
        return Objects.hash(carNumber);
    }

    @Override
    public void run() {
        synchronized (parkingLot) {
            while (parkingLot.parkingSpaceNumLeft() == 0) {
                try {
                    parkingLot.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                parkingLot.carIn(this);
        }
        parkingStartTime = System.currentTimeMillis();
        try {
            sleep(new Random().nextInt(20000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (parkingLot) {
            parkingLot.carOut(this);
            parkingLot.notifyAll();
        }
    }
}

